package org.shiro.action;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.shiro.bean.JsonResult;
import org.shiro.entity.Permission;
import org.shiro.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** @author HKC */
@Api(tags = "权限接口", value = "测试权限接口数据")
@RestController
@RequestMapping("/permission")
public class PermissionController {
  @Autowired private PermissionService permissionService;

  @ApiOperation(value = "添加权限", notes = "按照提交信息向数据库中插入数据")
  @PostMapping("/insert")
  @ResponseStatus(HttpStatus.CREATED)
  public JsonResult createPermission(@RequestBody Permission permission) {
    int res = permissionService.create(permission);
    if (res > 0) {
      return JsonResult.success("添加新权限成功", permission);
    }
    return JsonResult.failure("添加新权限失败");
  }

  @ApiOperation(value = "删除权限", notes = "按照提交数据从数据库中删除权限")
  @DeleteMapping("/{id}")
  public JsonResult removePermission(@PathVariable("id") Long id) {
    int res = permissionService.remove(id);
    if (res > 0) {
      return JsonResult.success("删除权限成功");
    }
    return JsonResult.failure("删除权限失败");
  }

  @ApiOperation(value = "查找权限", notes = "按照提交信息从数据库里获取数据")
  @GetMapping("/{id}")
  public JsonResult loadPermission(@PathVariable("id") Long id) {
    Permission permission = permissionService.load(id);
    if (permission != null) {
      return JsonResult.success("查找权限成功", permission);
    }
    return JsonResult.failure("查找权限失败");
  }

  @ApiOperation(value = "更改权限信息", notes = "按照提交信息更改数据库数据信息")
  @PutMapping("/modity")
  public JsonResult modityPermission(@RequestBody Permission permission) {
    int res = permissionService.modity(permission);
    if (res > 0) {
      return JsonResult.success("更改权限信息成功", permission);
    }
    return JsonResult.failure("更改权限信息失败");
  }

  @ApiOperation(value = "查看所有权限", notes = "按照提交信息获取数据库中所有数据")
  @GetMapping("/queryAll")
  public JsonResult queryAll() {
    List<Permission> permissionList = permissionService.queryAll();
    if (permissionList != null && permissionList.size() > 0) {
      return JsonResult.success("获取所有数据成功", permissionList);
    }
    return JsonResult.failure("获取数据失败");
  }

  @ApiOperation(value = "树状结构展示",notes = "按照提交数据以树状结构展示")
  @GetMapping("/queryByTree")
  public JsonResult queryByTree(){
    List<Permission> permissionList = permissionService.queryByTree();
    if (permissionList != null && permissionList.size() > 0){
      return JsonResult.success("树状结构展示成功",permissionList);
    }
    return JsonResult.failure("树状结构展示失败");
  }
}
